import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Universal from '../components/Universal.vue'
import Money from '../components/Money.vue'
import Personal from '../components/Personal.vue'
import Finance from "../components/Finance.vue";
import Polyline from "../components/money/Polyline.vue";
import Pie from "../components/money/Pie.vue";
import Bar from "../components/money/Bar.vue";
import Registered from "../components/Registered.vue";
import Retrieve from "../components/Retrieve.vue";
import On from "../components/personal/On";
import Help from "../components/personal/Help";

Vue.use(VueRouter)

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

const routes = [
    {
        path: "/",
        redirect: "/universal"
    },
    {
        path: "/login",
        name: "login",
        component: Login
    },
    {
        path: "/registered",
        name: "registered",
        component: Registered,
    },
    {
        path: "/retrieve",
        name: "retrieve",
        component: Retrieve
    },
    {
        path: "/on",
        name: "on",
        component: On
    },
    {
        path: "/help",
        name: "help",
        component: Help
    },
    {
        // 下面的公共部分
        path: "/universal",
        name: "universal",
        component: Universal,
        children: [
            {
                //第一个按钮：主页
                path: "/home",
                name: "home",
                component: Home
            },
            {
                //第二个按钮，查询财政的信息
                path: "/finance",
                name: "finance",
                component: Finance,
                meta: {
                    requireAuth: true
                }
            },
            {
                //第三个按钮：财产可视化页（登录显示）
                path: "/money",
                name: "money",
                component: Money,
                meta: {
                    requireAuth: true
                },
                children: [{
                    path: "/polyline",
                    name: "polyline",
                    component: Polyline
                }, {
                    path: "/pie",
                    name: "pie",
                    component: Pie
                }, {
                    path: "/bar",
                    name: "bar",
                    component: Bar
                }
                ]
            },
            {
                //第四个按钮：个人页（登录显示）
                path: "/personal",
                name: "personal",
                component: Personal,
                meta: {
                    requireAuth: true
                }
            }
        ]
    },
]

const router = new VueRouter({
    // mode: 'history',
    mode: 'hash',
    routes
})

router.beforeEach((to, from, next) => {
    //使用js-cookie获得cookie
    let cookie = $cookies.get("token");
    let username = $cookies.get("username");
    //获得当前用户的token
    let token = sessionStorage.getItem("token");
    if (cookie) {
        sessionStorage.setItem("token", cookie);
        sessionStorage.setItem("username", username);
    }

    //登录页有token去主页，需要token和登录的页面如果没有权限去登录
    if (to.path === "/login") {
        if (token) {
            next("/universal");
        } else {
            next();
        }
    } else {
        if (to.meta.requireAuth) {
            if (token) {
                next();
            } else {
                next("/login");
            }
        } else {
            next();
        }
    }
})

export default router


